package com.example.service.impl;

import com.example.mapper.EmpMapper;
import com.example.pojo.Emp;
import com.example.pojo.PageBean;
import com.example.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //1.获取总记录数
//        Long count = empMapper.count();
//        //起始索引计算
//        Integer start = (page-1)*pageSize;
//        //2.分页查询结果
//         List<Emp> empList = empMapper.page(start, pageSize);
//        return new PageBean(count,empList);
//    }
@Override
public PageBean page(Integer page, Integer pageSize,
                     String name,
                     Short gender,
                     LocalDate begin,
                     LocalDate end
) {
    //1.设置分页参数
    PageHelper.startPage(page, pageSize);
    //2.查询
    List<Emp> list = empMapper.list(name, gender, begin, end);
    //2.封装查询结果
    Page<Emp> p = (Page<Emp>) list;
    return new PageBean(p.getTotal(), p.getResult());
}

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp) ;
    }

    @Override
    public Emp getById(Integer id) {
       Emp emp =empMapper.getById(id);
       return emp;
    }

    @Override
    public void update(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);

    }

    @Override
    public Emp login(Emp emp) {
     Emp emp1= empMapper.getByUsernameAndPassword(emp);
        return emp1;
    }
}
